#include "lessonselection.h"
#include "ui_lessonselection.h"
#include <QDir>

LessonSelection::LessonSelection(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::LessonSelection)
{
    m_ui->setupUi(this);

    refreshAvailableLessons();

}

/*
this function is used to add the available lessons to the available lessons view in the lesson selection view
*/
void LessonSelection::refreshAvailableLessons()
{
    m_ui->availableLessonList->clear();//clears the view

    QDir dir(QString("lessons/"));//sets the directory
    dir.setNameFilters(QStringList(QString("*.ls")));//sets the files to look for

    foreach (QString lessonName, dir.entryList())//runs for each file in the folder
        m_ui->availableLessonList->addItem(lessonName);//adds the list to the lesson view
}
/*
function si used when the user is don selecting the lessons they want
*/
void LessonSelection::doneSelecting()
{
    QStringList allVids;
    for (int i = 0; i < m_ui->chosenLessonList->count(); i++)//runs a loop for the number of lessons selected
    {
        QListWidgetItem* wi = m_ui->chosenLessonList->item(i);//creates a new QListWidgetItem from the lesson at the index
        QFile file(QString("lessons/").append(wi->text()));//opens the lesson file
        if (!file.open(QIODevice::ReadOnly))
            continue;
        while (!file.atEnd())//while there is still data in the file
            allVids += file.readLine().trimmed();//the video addresses are adde to the selected videos list
    }
    emit selectedTheseVids(allVids);//the signal is emmited with the list of addresses
}
/*
shifts the selected lesson to the right hand side list
*/
void LessonSelection::shiftRight()
{
    m_ui->chosenLessonList->addItem(m_ui->availableLessonList->takeItem(m_ui->availableLessonList->currentRow()));//shifting the lesson
}
/*
shifts all the lessons to the right hand side
*/
void LessonSelection::shiftAllRight()
{
    while (m_ui->availableLessonList->count() > 0)//runs while there are still lessons to be added
        m_ui->chosenLessonList->addItem(m_ui->availableLessonList->takeItem(0));//shifts the lesson to the right hand side list
}
/*
shifts the selected lesson to the left hand side list
*/
void LessonSelection::shiftLeft()
{
    m_ui->availableLessonList->addItem(m_ui->chosenLessonList->takeItem(m_ui->chosenLessonList->currentRow()));//shits the lesson to the left
}
/*
shifts all the lessons to the lefts hand side list
*/
void LessonSelection::shiftAllLeft()
{
    while (m_ui->chosenLessonList->count() > 0)//runs while there are still lessons to be shifted
        m_ui->availableLessonList->addItem(m_ui->chosenLessonList->takeItem(0));//shifts the lesson
}

LessonSelection::~LessonSelection()
{
    delete m_ui;
}
/*
function written by QT
*/
void LessonSelection::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
